package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.feign.CacheFeign;
import com.qianfeng.openapi.web.master.mapper.ApiSystemParamMapper;
import com.qianfeng.openapi.web.master.pojo.ApiMapping;
import com.qianfeng.openapi.web.master.pojo.ApiSystemParam;
import com.qianfeng.openapi.web.master.service.ApiSystemParamService;
import com.qianfeng.openplatform.common.SystemParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/3 8:31
 * @Version 1.0
 */
@Service
@Transactional
public class ApiSystemParamServiceImpl implements ApiSystemParamService {

    @Autowired
    private ApiSystemParamMapper apiSystemParamMapper;

    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public int insertApiSystemParam(ApiSystemParam apiSystemParam) {
        int i = apiSystemParamMapper.insertApiSystemParam(apiSystemParam);
        try {
            cacheFeign.sAdd(SystemParams.SYSTEMPARAMS,apiSystemParam.getName(),600);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateApiSystemParam(ApiSystemParam apiSystemParam) {
        //先查询
        ApiSystemParam mapping = apiSystemParamMapper.getApiSystemParamById(apiSystemParam.getId());

        if (apiSystemParam != null){
            //先移除之前的
            try {
                cacheFeign.sRemove(SystemParams.SYSTEMPARAMS,mapping.getName());
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        int i = apiSystemParamMapper.updateApiSystemParam(apiSystemParam);
        if(apiSystemParam.getState() == 1 && i==1){
            //更新成功，且状态为1
            try {
                //设置缓存10分钟
                cacheFeign.sAdd(SystemParams.SYSTEMPARAMS, apiSystemParam.getName(), 600);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return 1;

    }

    @Override
    public int deleteApiSystemParam(Integer id) {
        return apiSystemParamMapper.deleteApiSystemParam(id);
    }

    @Override
    public ApiSystemParam getApiSystemParamById(Integer id) {
        return apiSystemParamMapper.getApiSystemParamById(id);
    }

    @Override
    public void deleteApiSystemParams(Integer[] ids) {
        if (ids!=null || ids.length==0) {
            return;
        }
        for (Integer id : ids) {
            ApiSystemParam apiSystemParam = apiSystemParamMapper.getApiSystemParamById(id);
            apiSystemParam.setState(0);
            //更改状态为0
            int i = apiSystemParamMapper.updateApiSystemParam(apiSystemParam);
            if (i==1 && apiSystemParam!=null){
                try {
                    cacheFeign.sRemove(SystemParams.SYSTEMPARAMS,apiSystemParam.getName());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageInfo<ApiSystemParam> getAllApiSystemParamByPage(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<ApiSystemParam> apiSystemParamList = apiSystemParamMapper.getAllApiSystemParams();
        return new PageInfo<>(apiSystemParamList);
    }

    @Override
    public PageInfo<ApiSystemParam> selectApiSystemParamByCondition(Integer page, Integer limit, String name, Integer state) {
        PageHelper.startPage(page,limit);
        List<ApiSystemParam> apiSystemParamList = apiSystemParamMapper.selectApiSystemParamByCondition(name, state);
        return new PageInfo<>(apiSystemParamList);
    }
}
